package web.eshop.panels;

import domain.ImageEntry;
import domain.shop.Product;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import service.facade.customer.ShopCustomerService;
import web.commons.ImageThumbnail;
import web.eshop.SimpleProductEshopPage;

import java.util.HashMap;
import java.util.Map;

/**
 * Panel contains exactly one product.
 */
public final class ProductPanel extends ShopBasePanel {

    @SpringBean
    private ShopCustomerService shopCustomerService;

    public ProductPanel(String id, final ListItem<Product> item) {
        super(id);
        Map<String, Long> pageParams = new HashMap<String, Long>();
        pageParams.put("0", item.getModelObject().getId());
        Link link = new BookmarkablePageLink("link", SimpleProductEshopPage.class,
                new PageParameters(pageParams));
        link.add(new Label("name", new PropertyModel(item, "modelObject.name")));
        add(link);

        //price
        add(new Label("price", item.getModelObject().getPrice().toString()));

        //Image
        IModel<ImageEntry> image =
                new LoadableDetachableModel<ImageEntry>() {

                    @Override
                    protected ImageEntry load() {
                        return item.getModelObject().getMainImage();
                    }
                };
        link.add(new ImageThumbnail("image", image));


        //popis a tlacitko pridat do kosiku
        Form form = new Form("form");
        form.add(new Button("addButton") {

            @Override
            public void onSubmit() {
                shopCustomerService.addProductToBasket(item.getModelObject(), 1);
                info(getLocalizer().getString("product.added", this));
            }
        });
        add(form);
        add(new Label("shortContent", new PropertyModel<String>(
                item.getModel(), "shortContent")).setEscapeModelStrings(false));


    }
}
